package com.xsz.controller;


import com.xsz.entity.Meeting;
import com.xsz.service.MeetingService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
public class TestController {
    @Resource
    MeetingService meetingService;

    @Value("${fileStoreRootPath}")
    String filePath;

    @RequestMapping("/test")
    public String test(@RequestParam("id") String id) {
        System.out.println(id);
        Meeting meeting=meetingService.selectById(id);
        try {
            Map<String,String> dataMap = new HashMap<String,String>();
            dataMap.put("f1", meeting.getTitle());
            dataMap.put("f2", meeting.getAddress());
            dataMap.put("f3", meeting.getStartTime());
            dataMap.put("f4", meeting.getPeople());
            Configuration configuration = new Configuration();
            configuration.setDefaultEncoding("utf-8");
            //指定模板路径的第二种方式,我的路径是D:/      还有其他方式
            System.out.println("filePath==="+filePath);
            configuration.setDirectoryForTemplateLoading(new File(filePath));

            // 输出文档路径及名称
            File outFile = new File(filePath+File.separator+"会议记录.doc");

            meetingService.addDowById("C:/tmp/会议记录.doc",id);


            //以utf-8的编码读取ftl文件
            Template t =  configuration.getTemplate("会议记录模板.ftl","utf-8");
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"),10240);
            t.process(dataMap, out);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/meeting/showAll";
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam("id") String id) {
        System.out.println(id);
        meetingService.delete(id);
        return "redirect:/meeting/showAll";
    }

//    @RequestMapping("/dow")
//    public String dow(@RequestParam("id") String id) {
//
//    }
}
